﻿using System;
using System.Diagnostics;
using System.Windows.Input;

namespace MDrive
{
    public class RelayCommand : ICommand
    {
        readonly Func<bool> _canExecute;
        readonly Action _execute;

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action execute, Func<bool> canExecute = null)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        #region ICommand Members

        [DebuggerStepThrough]
        public Boolean CanExecute(Object o)
        {
            return _canExecute == null ? true : _canExecute();
        }

        /// <summary>
        /// Occurs when changes occur that affect whether or not the command should
        /// execute.  Note that this delegates to CommandManager.RequerySuggested, which
        /// holds a weak reference, so a strong reference to the event handler should
        /// be held by the calling class.  For efficiency, the event handler is also only
        /// added if there is a _canExecute function.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                {
                    CommandManager.RequerySuggested += value;
                }
            }
            remove
            {
                if (_canExecute != null)
                {
                    CommandManager.RequerySuggested -= value;
                }
            }
        }

        public void Execute(Object o)
        {
            _execute();
        }

        #endregion
    }



    /// <summary>
    /// A command whose sole purpose is to relay its functionality to other
    /// objects by invoking delegates. The default return value for the CanExecute
    /// method is 'true'.
    /// 
    /// Adapted from the MSDN Magazine article "WPF Apps With The Model-View-ViewModel
    /// Design Pattern" by Josh Smith.
    /// </summary>
    public class RelayCommand<T> : ICommand
    {
        #region Constructor

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");

            _execute = execute;
            _canExecute = canExecute;
        }

        #endregion // Constructor


        T GetT(object parameterObj)
        {
            T parameter = default(T);

            if (parameterObj is T)
                parameter = (T)parameterObj;
            else if (parameterObj != null && parameterObj.GetType().FullName != "MS.Internal.NamedObject")
                Debug.Fail("ERROR in RelayCommand<T>: Parameter is not of type T.\n",
                    string.Format("Parameter is a {0}.\n\nT is {1}.", parameterObj.GetType().ToString(), typeof(T).ToString()));

            return parameter;
        }

        [DebuggerStepThrough]
        public Boolean CanExecute(T parameter)
        {
            return _canExecute == null ? true : _canExecute(parameter);
        }

        public void Execute(T parameter)
        {
            _execute(parameter);
        }


        #region ICommand Members

        [DebuggerStepThrough]
        public Boolean CanExecute(Object parameterObj)
        {
            return CanExecute(GetT(parameterObj));
        }

        /// <summary>
        /// Occurs when changes occur that affect whether or not the command should
        /// execute.  Note that this delegates to CommandManager.RequerySuggested, which
        /// holds a weak reference, so a strong reference to the event handler should
        /// be held by the calling class.  For efficiency, the event handler is also only
        /// added if there is a _canExecute function.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                {
                    CommandManager.RequerySuggested += value;
                }
            }
            remove
            {
                if (_canExecute != null)
                {
                    CommandManager.RequerySuggested -= value;
                }
            }
        }

        public void Execute(Object parameterObj)
        {
            Execute(GetT(parameterObj));
        }

        #endregion // ICommand Members


        #region Fields

        readonly Action<T> _execute;
        readonly Predicate<T> _canExecute;

        #endregion // Fields
    }
}